//@ts-check
const { Passport } = require('@casstime/passport');

const passport = new Passport({
  clientId: 'cassdev',
  secretKey: '123456',
  origin: 'https://test-api.casstime.com'
})

// 当前登录的用户名和密码
const userLoginId = 'mandy';
const password = 'Cass1713';

let tokens = {};

async function init() {
  try {
    tokens = await passport.getTokensByPassword(userLoginId, password);
    console.log(`${userLoginId} 登录成功`, tokens);
  } catch (err) {
    console.error(`${userLoginId} 用户登录失败`, err);
    process.exit(1);
  }
}

init();

module.exports = {
  publicPath: '',
  devServer: {  // 参考 https://webpack.js.org/configuration/dev-server/
    port: 11011,  // 本地调试环境的端口号
    proxy: [
      {
        context: ['/terminal-api'],
        target: 'https://ec-test.casstime.com', // 实际请求到 https://ec-test.casstime.com/terminal-api/*
        // pathRewrite: { // 代理时，重写路径，当网关path和服务path不一致时可以配置pathRewrite
        //   '^/inquiryWeb': '/',
        // },
        changeOrigin: true,
        onProxyReq(proxyReq, req, res) {
          // 为代理的请求增加authorization头
          proxyReq.setHeader('Authorization', `bearer ${tokens.access_token}`);
        }
      },
      {
        context: ['/users'],
        target: 'http://localhost:22346/', // 实际请求到 https://ec-test.casstime.com/terminal-api/*
        // pathRewrite: { // 代理时，重写路径，当网关path和服务path不一致时可以配置pathRewrite
        //   '^/inquiryWeb': '/',
        // },
        changeOrigin: true,
        onProxyReq(proxyReq, req, res) {
          // 为代理的请求增加authorization头
          proxyReq.setHeader('Authorization', `bearer ${tokens.access_token}`);
        }
      }
    ]
  }
};
